#include "smartwidget.h"

SmartWidget::SmartWidget(QWidget *parent) : QWidget(parent)
{
    // 1.初始化标志
    // 1.1 控制灯泡的变量
    isShow1 = false;
    isShow2 = false;
    isShow3 = false;
    isShow4 = false;
    isShow5 = false;
    isShow6 = false;
    isShow7 = false;
    // 1.2 控制空调的变量
    isOpen1 = false;
    isOpen2 = false;
    isOpen3 = false;

    // 2.创建按钮
    light_btn1 = new QPushButton("开 灯", this);
    light_btn2 = new QPushButton("开 灯", this);
    light_btn3 = new QPushButton("开 灯", this);
    light_btn4 = new QPushButton("开 灯", this);
    light_btn5 = new QPushButton("开 灯", this);
    light_btn6 = new QPushButton("开 灯", this);
    light_btn7 = new QPushButton("开 灯", this);
    air_btn1 = new QPushButton("开空调", this);
    air_btn2 = new QPushButton("开空调", this);
    air_btn3 = new QPushButton("开空调", this);

    // 2.创建服务器
    tcpServer = tcpServerSingleton::create_tcpServer();

    // 3.信号/槽函数
    this->connect(tcpServer, &tcpServerSingleton::sig_recv, this, &SmartWidget::slot_update_status);

    this->connect(light_btn1, &QPushButton::clicked, this, [this](){
        if(isShow1) // 按下按钮时，isShow1为真，说明为当前状态为开灯，实际动作为关灯，发送关灯信号
        {
            light_btn1->setText("开 灯");
            emit sig_send("110000");
        }
        else
        {
            light_btn1->setText("关 灯");
            emit sig_send("111000");
        }
        isShow1 = !isShow1;
        update();
    });

    this->connect(light_btn2, &QPushButton::clicked, this, [this](){
        if(isShow2) // 按下按钮时，isShow2为真，说明为当前状态为开灯，实际动作为关灯，发送关灯信号
        {
            light_btn2->setText("开 灯");
            emit sig_send("120000");
        }
        else
        {
            light_btn2->setText("关 灯");
            emit sig_send("121000");
        }
        isShow2 = !isShow2;
        update();
    });

    this->connect(light_btn3, &QPushButton::clicked, this, [this](){
        if(isShow3) // 按下按钮时，isShow3为真，说明为当前状态为开灯，实际动作为关灯，发送关灯信号
        {
            light_btn3->setText("开 灯");
            emit sig_send("130000");
        }
        else
        {
            light_btn3->setText("关 灯");
            emit sig_send("131000");
        }
        isShow3 = !isShow3;
        update();
    });

    this->connect(light_btn4, &QPushButton::clicked, this, [this](){
        if(isShow4) // 按下按钮时，isShow4为真，说明为当前状态为开灯，实际动作为关灯，发送关灯信号
        {
            light_btn4->setText("开 灯");
            emit sig_send("140000");
        }
        else
        {
            light_btn4->setText("关 灯");
            emit sig_send("141000");
        }
        isShow4 = !isShow4;
        update();
    });

    this->connect(light_btn5, &QPushButton::clicked, this, [this](){
        if(isShow5) // 按下按钮时，isShow5为真，说明为当前状态为开灯，实际动作为关灯，发送关灯信号
        {
            light_btn5->setText("开 灯");
            emit sig_send("150000");
        }
        else
        {
            light_btn5->setText("关 灯");
            emit sig_send("151000");
        }
        isShow5 = !isShow5;
        update();
    });

    this->connect(light_btn6, &QPushButton::clicked, this, [this](){
        if(isShow6) // 按下按钮时，isShow6为真，说明为当前状态为开灯，实际动作为关灯，发送关灯信号
        {
            light_btn6->setText("开 灯");
            emit sig_send("160000");
        }
        else
        {
            light_btn6->setText("关 灯");
            emit sig_send("161000");
        }
        isShow6 = !isShow6;
        update();
    });

    this->connect(light_btn7, &QPushButton::clicked, this, [this](){
        if(isShow7) // 按下按钮时，isShow7为真，说明为当前状态为开灯，实际动作为关灯，发送关灯信号
        {
            light_btn7->setText("开 灯");
            emit sig_send("170000");
        }
        else
        {
            light_btn7->setText("关 灯");
            emit sig_send("171000");
        }
        isShow7 = !isShow7;
        update();
    });

    this->connect(air_btn1, &QPushButton::clicked, this, [this](){
        if(isOpen1)
        {
            air_btn1->setText("开空调");
            emit sig_send("210000");
        }
        else
        {
            air_btn1->setText("关空调");
            emit sig_send("211000");
        }
        isOpen1 = !isOpen1;
        update();
    });

    this->connect(air_btn2, &QPushButton::clicked, this, [this](){
        if(isOpen2)
        {
            air_btn2->setText("开空调");
            emit sig_send("220000");
        }
        else
        {
            air_btn2->setText("关空调");
            emit sig_send("221000");
        }
        isOpen2 = !isOpen2;
        update();
    });

    this->connect(air_btn3, &QPushButton::clicked, this, [this](){
        if(isOpen3)
        {
            air_btn3->setText("开空调");
            emit sig_send("230000");
        }
        else
        {
            air_btn3->setText("关空调");
            emit sig_send("231000");
        }
        isOpen3 = !isOpen3;
        update();
    });
}

void SmartWidget::slot_update_status(const QString &num)
{
    if(num == "000") // 关闭所有设备
    {
        isShow1 = false;
        isShow2 = false;
        isShow3 = false;
        isShow4 = false;
        isShow5 = false;
        isShow6 = false;
        isShow7 = false;
        isOpen1 = false;
        isOpen2 = false;
        isOpen3 = false;
    }
    else if(num == "110") // 关闭卧室1的灯
    {
        light_btn1->setText("开  灯");
        isShow1 = false;
    }
    else if(num == "111") // 打开卧室1的灯
    {
        light_btn1->setText("关  灯");
        isShow1 = true;
    }
    else if(num == "120") // 关闭卧室2的灯
    {
        light_btn2->setText("开  灯");
        isShow2 = false;
    }
    else if(num == "121") // 打开卧室2的灯
    {
        light_btn2->setText("关  灯");
        isShow2 = true;
    }
    else if(num == "130") // 关闭客厅的灯
    {
        light_btn3->setText("开  灯");
        isShow3 = false;
    }
    else if(num == "131") // 打开客厅的灯
    {
        light_btn3->setText("关  灯");
        isShow3 = true;
    }
    else if(num == "140") // 关闭过道的灯
    {
        light_btn4->setText("开  灯");
        isShow4 = false;
    }
    else if(num == "141") // 打开过道的灯
    {
        light_btn4->setText("关  灯");
        isShow4 = true;
    }
    else if(num == "150") // 关闭卫生间的灯
    {
        light_btn5->setText("开  灯");
        isShow5 = false;
    }
    else if(num == "151") // 打开卫生间的灯
    {
        light_btn5->setText("关  灯");
        isShow5 = true;
    }
    else if(num == "160") // 关闭厨房的灯
    {
        light_btn6->setText("开  灯");
        isShow6 = false;
    }
    else if(num == "161") // 打开厨房的灯
    {
        light_btn6->setText("关  灯");
        isShow6 = true;
    }
    else if(num == "170") // 关闭阳台的灯
    {
        light_btn7->setText("开  灯");
        isShow7 = false;
    }
    else if(num == "171") // 打开阳台的灯
    {
        light_btn7->setText("关  灯");
        isShow7 = true;
    }
    else if(num == "210") // 关闭卧室1的空调
    {
        air_btn1->setText("开空调");
        isOpen1 = false;
    }
    else if(num == "211") // 打开卧室1的空调
    {
        air_btn1->setText("关空调");
        isOpen1 = true;
    }
    else if(num == "220") // 关闭卧室2的空调
    {
        air_btn2->setText("开空调");
        isOpen2 = false;
    }
    else if(num == "221") // 打开卧室2的空调
    {
        air_btn2->setText("关空调");
        isOpen2 = true;
    }
    else if(num == "230") // 关闭客厅的空调
    {
        air_btn3->setText("开空调");
        isOpen3 = false;
    }
    else if(num == "231") // 打开客厅的空调
    {
        air_btn3->setText("关空调");
        isOpen3 = true;
    }

    update();
}

void SmartWidget::paintEvent(QPaintEvent *e)
{
    // 1.创建画家，并绑定窗口
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing); // 抗锯齿

    // 2.添加图片背景
    QPixmap pixmap("://image/smart.jpg");
    painter.drawPixmap(0, 0, this->width(), this->height(), pixmap); // 绘制图片背景

    // 3.添加灯
    QBrush light_brush;
    light_brush.setStyle(Qt::SolidPattern);

    if(isShow1)
        light_brush.setColor(Qt::yellow);
    else
        light_brush.setColor(Qt::darkYellow);
    painter.setBrush(light_brush);
    painter.drawEllipse(this->width() * 0.1, this->height() * 0.22, 30, 30);

    if(isShow2)
        light_brush.setColor(Qt::yellow);
    else
        light_brush.setColor(Qt::darkYellow);
    painter.setBrush(light_brush);
    painter.drawEllipse(this->width() * 0.1, this->height() * 0.62, 30, 30);

    if(isShow3)
        light_brush.setColor(Qt::yellow);
    else
        light_brush.setColor(Qt::darkYellow);
    painter.setBrush(light_brush);
    painter.drawEllipse(this->width() * 0.55, this->height() * 0.3, 30, 30);

    if(isShow4)
        light_brush.setColor(Qt::yellow);
    else
        light_brush.setColor(Qt::darkYellow);
    painter.setBrush(light_brush);
    painter.drawEllipse(this->width() * 0.5, this->height() * 0.6, 30, 30);

    if(isShow5)
        light_brush.setColor(Qt::yellow);
    else
        light_brush.setColor(Qt::darkYellow);
    painter.setBrush(light_brush);
    painter.drawEllipse(this->width() * 0.7, this->height() * 0.6, 30, 30);

    if(isShow6)
        light_brush.setColor(Qt::yellow);
    else
        light_brush.setColor(Qt::darkYellow);
    painter.setBrush(light_brush);
    painter.drawEllipse(this->width() * 0.65, this->height() * 0.86, 30, 30);

    if(isShow7)
        light_brush.setColor(Qt::yellow);
    else
        light_brush.setColor(Qt::darkYellow);
    painter.setBrush(light_brush);
    painter.drawEllipse(this->width() * 0.89, this->height() * 0.31, 30, 30);

    // 4.添加空调
    QBrush air_brush;
    air_brush.setStyle(Qt::SolidPattern);

    if(isOpen1)
        air_brush.setColor(Qt::green);
    else
        air_brush.setColor(Qt::darkBlue);    
    painter.setBrush(air_brush);
    painter.drawRect(this->width() * 0.32 - 30, this->height() * 0.22, 60, 30);

    if(isOpen2)
        air_brush.setColor(Qt::green);
    else
        air_brush.setColor(Qt::darkBlue);
    painter.setBrush(air_brush);
    painter.drawRect(this->width() * 0.32 - 30, this->height() * 0.62, 60, 30);

    if(isOpen3)
        air_brush.setColor(Qt::green);
    else
        air_brush.setColor(Qt::darkBlue);
    painter.setBrush(air_brush);
    painter.drawRect(this->width() * 0.68 - 30, this->height() * 0.3, 60, 30);
}

void SmartWidget::resizeEvent(QResizeEvent *e)
{
    light_btn1->setGeometry(this->width() * 0.1 - 30, this->height() * 0.22 + 40, 90, 30);
    light_btn2->setGeometry(this->width() * 0.1 - 30, this->height() * 0.62 + 40, 90, 30);
    light_btn3->setGeometry(this->width() * 0.55 - 30, this->height() * 0.3 - 40, 90, 30);
    light_btn4->setGeometry(this->width() * 0.5 - 30, this->height() * 0.6 + 40, 90, 30);
    light_btn5->setGeometry(this->width() * 0.7 - 30, this->height() * 0.6 - 40, 90, 30);
    light_btn6->setGeometry(this->width() * 0.65 - 100, this->height() * 0.86, 90, 30);
    light_btn7->setGeometry(this->width() * 0.89 - 30, this->height() * 0.31 - 40, 90, 30);
    air_btn1->setGeometry(this->width() * 0.32 - 45, this->height() * 0.22 + 40, 90, 30);
    air_btn2->setGeometry(this->width() * 0.32 - 45, this->height() * 0.62 + 40, 90, 30);
    air_btn3->setGeometry(this->width() * 0.68 - 40, this->height() * 0.3 - 40, 80, 30);
}
